<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Login_lib {
    function __construct() {
		$this->CI = & get_instance();
        log_message('debug', "Login Class Initialized");
    }

	function login($data) {
		
		$this->CI->load->model('users_model');
		$this->CI->load->model('role_model');	
		$obj = new stdClass();	
		$i = new stdClass();	


		$obj->user=$data['user'];
		$obj->password=$data['password'];
				
		$info	=	$this->CI->users_model->check_login_user($obj);
		$role_info	=	$this->CI->role_model->get_info($info->role_id);
		
		if(!empty($info)){
			
			$obj->state=0;
			$this->write_login_log($obj);
			
			$i->sercet_level=	$role_info->sercet_level;
			$i->is_public	=	$role_info->is_public;
			$i->important	=	$role_info->important;
			$i->is_see		=	$role_info->is_see;
			
			$i->users_id=$info->users_id;
			$i->name=$info->name;
			$i->shop_point=$info->shop_point_id;
			$i->role=$info->role_id;
			
			$this->set_session($i);
			$obj->state=1;
			$this->write_login_log($obj);
			
			return true;
		}	
		
		$obj->state=0;
		$this->write_login_log($obj);
		return false;			
	}

	function set_session($obj) {
		$this->CI->load->library('session');
		
		$this->CI->session->set_userdata('sercet_level', $obj->sercet_level);
		$this->CI->session->set_userdata('is_public', $obj->is_public);	
		$this->CI->session->set_userdata('important',$obj->important);
		$this->CI->session->set_userdata('is_see',$obj->is_see);
		
		
		$this->CI->session->set_userdata('users_id', $obj->users_id);
		$this->CI->session->set_userdata('user', $obj->name);	
		$this->CI->session->set_userdata('shop_point_id',$obj->shop_point);
		$this->CI->session->set_userdata('role',$obj->role);
		
		
		$this->CI->load->model('role_model');
		$role=$this->CI->role_model->get_info($obj->role);
		$this->CI->session->set_userdata('purview',$role->purview);
	}
	
	
	//主管！為劉陳傳
	function get_head() {
		
		$this->CI->load->model('users_model');								
		$head = $this->CI->users_model->get_user_by_user("劉陳傳");
		
		return $head;
	}
	
	
	function is_login() {
		$users_id=$this->CI->session->userdata('users_id');
		if($users_id>0)
			return true;
		else
			return false;
	}
	
	public function purview()
    {
		/*$p=$this->CI->session->userdata('purview');
		if($p)
			return $p;
		else
			return "";*/
		return $this->CI->session->userdata('purview');
    }

    public function role()
    {
        return $this->CI->session->userdata('role');
    }

	public function users_id()
    {
        return $this->CI->session->userdata('users_id');
    }
	public function shop_point_id()
    {
        return $this->CI->session->userdata('shop_point_id');
    }
    public function users_name()
    {
        return $this->CI->session->userdata('name') ? $this->CI->session->userdata('name') : $this->CI->session->userdata('user');
    }

	public function logout()
    {
        $this->CI->session->sess_destroy();
		redirect("/");
    }	
	function write_login_log($obj) {
		$this->CI->load->model('login_log_model', 'login_log');
		$data['name'] = $obj->user;
		$data['password'] = $obj->password;
		$data['state']=$obj->state;
		$this->CI->login_log->login_insert($data);
	}
	
}
// END Login class
/* End of file Login.php */
